package com.dayatang.android.config;

import java.util.concurrent.ExecutionException;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.telephony.TelephonyManager;
import android.telephony.gsm.GsmCellLocation;
import android.util.Log;

import com.dayatang.android.Task.BaseTask;
import com.dayatang.android.Task.InitDataServiceTask;
import com.dayatang.android.Task.LocationTask;

public class LocationConfig {
	
	public static void reportLocation(String serverAddress, String username, Context context) {
		try {
			Log.i("123", "a");
			double[] ds = getLongitudeAndLatitude(context);
			Log.i("123", "b");
			String url = serverAddress + "/remote/report-loaction.action?username=" + username + "&location.longitude=" + ds[0] + "&location.latitude=" + ds[1];
			new InitDataServiceTask(context).execute(url);
			Log.i("123", "c");
		} catch (JSONException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		}
	}
	
	// 获取经、纬度
	public static double[] getLongitudeAndLatitude(Context context) throws JSONException, InterruptedException, ExecutionException {
		double[] ds = new double[2];
		if (IsGetLocation(context)) {
			Location location = getLocationByGPS(context);
			ds[0] = location.getLongitude();
			ds[1] = location.getLatitude();
		} else {
			JSONObject jsonObject = getLocationByNetwork(context);
			JSONObject subjson = new JSONObject(jsonObject.getString("location"));
			ds[0] = subjson.getDouble("longitude");
			ds[1] = subjson.getDouble("latitude");
		}
		return ds;
	}
	
	// 通过经纬度获取地理信息
	public static String getLocalByItude(Context context, String latitude, String longitude) throws InterruptedException, ExecutionException, JSONException {
		String url = "http://maps.google.cn/maps/geo?key=abcdefg&q=" + latitude + "," + longitude;
		String data = new BaseTask(context).execute(url).get();
		JSONObject jsonobject = new JSONObject(data);
		JSONArray jsonArray = new JSONArray(jsonobject.get("Placemark").toString());
		return jsonArray.getJSONObject(0).getString("address");
	}

	
	// 是否开启GPS功能
	public static boolean IsGetLocation(Context context) {
		LocationManager lm =(LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
		return lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
	}
	
	// 获取GPS位置
	public static Location getLocationByGPS(Context context) {
		
		LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

		Criteria criteria = new Criteria();
		criteria.setAccuracy(Criteria.ACCURACY_FINE);  // 精准度良好
		criteria.setAltitudeRequired(false);
		criteria.setBearingRequired(false);
		criteria.setCostAllowed(true);
		criteria.setPowerRequirement(Criteria.POWER_LOW);
		String provider = locationManager.getBestProvider(criteria,true);

		Location location = locationManager.getLastKnownLocation(provider);
		
		if (location == null) {
			location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
		}
		
		return location;
	}
	
	// 获取手机基站位置
	public static JSONObject getLocationByNetwork(Context context) throws JSONException, InterruptedException, ExecutionException {
		TelephonyManager mTelNet = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
		GsmCellLocation location = (GsmCellLocation) mTelNet.getCellLocation();
		String result = new LocationTask(context).execute("http://www.google.com/loc/json",getJsonDataBySIM(location.getCid(),location.getLac())).get();
		return new JSONObject(result);
	}
	
	private static String getJsonDataBySIM(int cid, int lac) throws JSONException {
		
		JSONObject holder = new JSONObject();
		holder.put("version", "1.1.0");
		holder.put("host", "maps.google.com");
		holder.put("address_language", "zh_CN");
		holder.put("request_address", true);

		JSONObject tower = new JSONObject();
		tower.put("cell_id", cid);
		tower.put("location_area_code", lac);

		JSONArray towerarray = new JSONArray();
		towerarray.put(tower);

		holder.put("cell_towers", towerarray);
		return holder.toString();
	}
	
}
